#! /bin/bash

# 获取环境变量
PLAY_HOME=/opt/playground

# 配置kylin.properties
function configureKylinProp()
{
# 获取kylin配置文件
kl_config=$1

echo "正在配置kylin.properties文件"

 #在frames.txt中查看hive信息
 hiveInfo=`egrep "hive" $PLAY_HOME/frames.txt`
 hiveNode=`echo $hiveInfo | cut -d " " -f3`

sed -i "s/#kylin.source.hive.client=cli/kylin.source.hive.client=beeline/g" $kl_config
sed -i "s/##kylin.source.hive.beeline-params=-n root --hiveconf hive.security.authorization.sqlstd.confwhitelist.append='mapreduce.job.*|dfs.*' -u jdbc:hive2:\/\/localhost:10000/kylin.source.hive.beeline-params=-n root -u jdbc:hive2:\/\/$hiveNode:10000/g" $kl_config
}

# 安装Kylin
function installKylin()
{
 #1.在frames.txt中查看是否需要安装kylin
 klInfo=`egrep "^apache-kylin" $PLAY_HOME/frames.txt`

 kl=`echo $klInfo | cut -d " " -f1`
 isInstall=`echo $klInfo | cut -d " " -f2`
 klNodes=`echo $klInfo | cut -d " " -f3` 
 masterNode=`echo $klInfo | cut -d " " -f4` 
 node=`hostname`

 #是否安装
 if [[ $isInstall = "true" && $klNodes =~ $node ]];then
    
     #2.查看$PLAY_HOME/frames目录下是否有kylin安装包
     klIsExists=`find $PLAY_HOME/frames -name $kl`

     if [[ ${#klIsExists} -ne 0 ]];then

          if [[ ! -d /opt/app ]];then
              mkdir /opt/app && chmod -R 775 /opt/app
          fi

          #删除旧的
          kl_home_old=`find /opt/app -maxdepth 1 -name "apache-kylin*"`
          for i in $kl_home_old;do
                rm -rf $i
          done

          #3.解压到指定文件夹/opt/app中
          echo "开始解压kylin安装包"
          tar -zxvf $klIsExists -C /opt/app >& /dev/null
          echo "kylin安装包解压完毕"

          kl_home=`find /opt/app -maxdepth 1 -name "apache-kylin*"`

          # 当前kylin需要安装在hive所在节点
          # 需要从hive节点拷贝所需的jdbc包
          hive_home=`find /opt/app -maxdepth 1 -name "*hive*"`
          hive_jdbc_dir=`find $hive_home/jdbc -maxdepth 1 -name "hive-jdbc*"`
          cp $hive_jdbc_dir $kl_home/lib

          #4.配置kylin.properties文件
          configureKylinProp $kl_home/conf/kylin.properties

          #7.配置KYLIN_HOME
          profile=/etc/profile
          sed -i "/^export KYLIN_HOME/d" $profile
          echo "export KYLIN_HOME=$kl_home" >> $profile
          
          #8.配置PATH
          sed -i "/^export PATH=\$PATH:\$KYLIN_HOME\/bin/d" $profile
          echo "export PATH=\$PATH:\$KYLIN_HOME/bin" >> $profile
          
          # 添加Hive所需依赖  
          sed -i "/^export hive_dependency/d" $profile
          #   export hive_dependency=$HIVE_HOME/conf:$HIVE_HOME/lib:$HIVE_HOME/hcatalog/share/hcatalog/hive-hcatalog-core-2.3.7.jar:$HIVE_HOME/lib/hive-exec-2.3.7.jar:$HIVE_HOME/lib/hive-metastore-2.3.7.jar
          
          # 获取HIVE_HOME
          HIVE_HOME=`egrep "^export HIVE_HOME=" $profile`

          if [ -z "$HIVE_HOME" ]; then
              echo "ERROR: HIVE_HOME variable is not set. Please install Hive and set HIVE_HOME."
              exit 1
          fi

          echo "hive_lib=\$(find -L \${HIVE_HOME}/lib -name '*.jar' ! -name '*druid*' ! -name '*slf4j*' ! -name '*avatica*' ! -name '*calcite*' ! -name '*jackson-datatype-joda*' ! -name '*derby*' ! -name '*icu4j*' -printf '%p:' | sed 's/:$//')" >> $profile
          echo "hive_hcatalog_core=\$(find -L \${HIVE_HOME}/hcatalog/share/hcatalog -name 'hive-hcatalog-core-*.jar')" >> $profile
          echo "export hive_dependency=\$HIVE_HOME/conf:\$hive_lib:\$hive_hcatalog_core" >> $profile

          #9.更新/etc/profile文件
          source /etc/profile && source /etc/profile
          
          # 将Hive依赖的jar包添加到HBASE_CLASSPATH中
          sed -i "s|export HBASE_CLASSPATH_PREFIX=\${KYLIN_HOME}/conf:\${KYLIN_HOME}/lib/\*:\${KYLIN_HOME}/ext/\*:\${HBASE_CLASSPATH_PREFIX}|export HBASE_CLASSPATH_PREFIX=\${KYLIN_HOME}/conf:\${KYLIN_HOME}/lib/\*:\${KYLIN_HOME}/ext/\*:\${hive_dependency}:\${HBASE_CLASSPATH_PREFIX}|" "$kl_home/bin/kylin.sh"

          echo "Kylin安装成功！"
        #   mr-jobhistory-daemon.sh start historyserver
     else
         echo "$PLAY_HOME/frames目录下没有kylin安装包"
     fi
 else
     echo "kylin不允许安装在当前节点，请检查配置文件！"
 fi

}

installKylin
